﻿using System;
using System.Collections.Generic;
using System.Linq;
using UtilZ.Dotnet.Ex.Attributes;
using UtilZ.Dotnet.Ex.Model;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// 常量辅助类
    /// </summary>
    public class ConstantHelper
    {
        /// <summary>
        /// 断言类型T为类类型
        /// </summary>
        /// <param name="type">类型</param>
        public static void AssertClass(Type type)
        {
            if (!type.IsClass)
            {
                throw new ArgumentException(string.Format("类型:{0}不是类类型", type.FullName));
            }
        }

        /// <summary>
        /// 获取常量字段特性信息列表
        /// </summary>
        /// <typeparam name="T">常量定义类</typeparam>
        /// <returns>PropertyFieldInfo列表</returns>
        public static List<PropertyFieldMemberInfo> GetConstantFieldInfoList<T>()
        {
            return GetConstantFieldInfoList(typeof(T));
        }

        /// <summary>
        /// 获取常量字段特性信息列表
        /// </summary>
        /// <param name="type">类类型</param>
        /// <returns>PropertyFieldInfo列表</returns>
        public static List<PropertyFieldMemberInfo> GetConstantFieldInfoList(Type type)
        {
            AssertClass(type);

            var fieldInfoArr = type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
            Type displayNameExAttributeType = typeof(DisplayNameExAttribute);
            Type ignoreAttributeType = typeof(IgnoreAttribute);
            object[] attrObjArr = null;

            List<Tuple<DisplayNameExAttribute, object>> attriItems = new List<Tuple<DisplayNameExAttribute, object>>();
            object value = null;
            DisplayNameExAttribute dneAtt = null;

            foreach (var fieldInfo in fieldInfoArr)
            {
                attrObjArr = fieldInfo.GetCustomAttributes(ignoreAttributeType, false);
                if (attrObjArr != null && attrObjArr.Length > 0)
                {
                    //标记了忽略特性
                    continue;
                }

                attrObjArr = fieldInfo.GetCustomAttributes(displayNameExAttributeType, false);
                value = fieldInfo.GetValue(null);

                if (attrObjArr.Length == 0)
                {
                    dneAtt = new DisplayNameExAttribute(fieldInfo.Name, fieldInfo.Name);
                }
                else
                {
                    dneAtt = (DisplayNameExAttribute)attrObjArr[0];
                }

                attriItems.Add(Tuple.Create<DisplayNameExAttribute, object>(dneAtt, value));
            }

            attriItems = attriItems.OrderBy(new Func<Tuple<DisplayNameExAttribute, object>, int>((item) => { return item.Item1.OrderIndex; })).ToList();

            var list = new List<PropertyFieldMemberInfo>();
            foreach (var enumAttriItem in attriItems)
            {
                list.Add(new PropertyFieldMemberInfo(enumAttriItem.Item1.DisplayName, enumAttriItem.Item2, enumAttriItem.Item1.Description, enumAttriItem.Item1));
            }

            return list;
        }
    }
}
